<?php

class Hydrum
{

    /**
     * @var Hydrum_Application
     */
    protected static $_application = null;

    /**
     * Find application by host and run it
     *
     * @param string $host
     * @param array $runtimeConfig
     */
    public static function run($host, array $runtimeConfig = array())
    {
        $config = new Hydrum_Application_Config($host);

        if (!empty($runtimeConfig)) {
            if (!$runtimeConfig instanceof Zend_Config) {
                $runtimeConfig = new Zend_Config($runtimeConfig);
            }

            $config->merge($runtimeConfig);
        }

        define('APPLICATION_PATH', $config->getApplicationPath());
        define('APPLICATION_TMP_PATH', APPLICATION_PATH . DS . 'tmp');

        self::$_application = new Hydrum_Application(APPLICATION_ENV, $config);

        return self::$_application->bootstrap()->run();

    }

    /**
     * Simple autoload function
     * returns true if the class was loaded, otherwise false
     *
     * @param string $className
     * @return boolean
     */
    public static function autoload($className)
    {
        $paths = get_include_path();
        $paths = explode(PATH_SEPARATOR, $paths);

        foreach ($paths as $path) {
            $file = $path . DS . str_replace(array('_', '\\'), DS, $className) .
                '.php';

            if (file_exists($file)) {
                require_once $file;

                return true;
            }
        }

        return false;
    }

    /**
     * Return currently running application
     *
     * @return Hydrum_Application
     */
    public static function getApplication()
    {
        return self::$_application;
    }

    /**
     * Return application resource by name
     *
     * @param Zend_Application_Resource_ResourceAbstract $name
     */
    public static function getResource($name)
    {
        return self::getApplication()->getBootstrap()->getResource($name);
    }

    public static function getModelConfig($pathname)
    {
        $pathname = new Hydrum_Model_Pathname($pathname);

        $modules = self::getResource('modules');

        $moduleName = $pathname->getModuleName();

        $module = $modules->get($moduleName);

        $modelName = $pathname->getModelName();

        return $module->getModelConfig($modelName);
    }
}
